require "import"
import "android.app.*"
import "android.content.*"
import "android.view.accessibility.AccessibilityNodeInfo"

-- Obtém o contexto correto
local context = activity or service or Application:getContext()

-- Cria o intent para abrir o Telegram Web
local intent = Intent(Intent.ACTION_MAIN)
intent.setComponent(ComponentName("org.telegram.messenger.web", "org.telegram.ui.ExternalActionActivity"))

-- Inicia a atividade
context.startActivity(intent)

-- Função para realizar o clique direto no botão "Limpar Cache"
function clickButton(text)
    if service.click({
        {text, 100}  -- Tempo reduzido para 100ms para resposta mais rápida
    }) then
        return true
    end
    return false
end

-- Função para tentar clicar no botão com um tempo de espera menor
function tryClickButtonWithDelay(text, delay, callback)
    task(delay, function()
        if clickButton(text) then
            if callback then
                callback() -- Executa a ação seguinte (voltar à tela inicial)
            end
        end
    end)
end

-- Aguarda 1 segundo para clicar no botão "Limpar Cache"
task(1000, function()
    if clickButton("Limpar Cache") then
        -- Aguarda 1 segundo para a confirmação da pop-up e clica novamente
        tryClickButtonWithDelay("Limpar Cache", 1000, function()
            -- Voltar para a tela inicial após confirmar a limpeza
            local homeIntent = Intent(Intent.ACTION_MAIN)
            homeIntent.addCategory(Intent.CATEGORY_HOME)
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(homeIntent)
        end)
    end
end)

return true